package com.mgy.im;

import com.alibaba.fastjson.JSON;
import com.mgy.im.annotation.NettyWebSocket;
import com.mgy.im.pojo.Session;
import com.mgy.im.standard.WebSocketHandler;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * xxx
 *
 * @author maguoyong
 * @date 2020/6/14
 */
@NettyWebSocket
public class MyWebSocketHandler implements WebSocketHandler {
    @Override
    public void afterConnectionEstablished(Session session) throws Exception {
        System.out.println("已连接");
    }

    @Override
    public void handleMessage(Session session, Object obj) throws Exception {
        System.out.println(obj);

        Map<String, Object> map = new HashMap<>();
        map.put("message", obj.toString());
        map.put("createTime", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> from = new HashMap<>();
        from.put("id", "1111");
        from.put("phone", "13124323456");
        from.put("password", "密码");
        from.put("code", "aaa");
        from.put("headImg", "/static/img/head/4.jpg");
        map.put("from", from);

        String text = JSON.toJSONString(map);

        session.sendText(text);
    }

    @Override
    public void handleError(Session session, Throwable throwable) throws Exception {
        System.out.println("出现错误:" + throwable.getMessage());
    }

    @Override
    public void afterConnectionClosed(Session session, String reason) throws Exception {
        System.out.println("连接关闭");
    }

}
